package org.wu.framework.web.spring.ui.info;

import org.wu.framework.web.ui.LazyUI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.wu.framework.core.utils.ObjectUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : Jia wei Wu
 * @version 1.0
 * describe :
 * @date : 2021/6/27 7:55 下午
 */
@Slf4j
public class LazyWebAddressInfo implements InitializingBean {

    private final ServerProperties serverProperties;
    private final ConfigurableApplicationContext configurableApplicationContext;
    private final List<LazyUI> lazyUIList;

    public LazyWebAddressInfo(ServerProperties serverProperties, ConfigurableApplicationContext configurableApplicationContext, List<LazyUI> lazyUIList) {
        this.serverProperties = serverProperties;
        this.configurableApplicationContext = configurableApplicationContext;
        this.lazyUIList = lazyUIList;
    }

    @Override
    public void afterPropertiesSet() throws UnknownHostException {
        Integer port = serverProperties.getPort();
        if (ObjectUtils.isEmpty(port)) {
            port = 8080;
        }
        String contextPath = "";
        ServerProperties.Servlet servlet = serverProperties.getServlet();
        if (servlet != null && null != servlet.getContextPath()) {
            contextPath = servlet.getContextPath();
        }
//        log.info("加载 web-swagger2地址: http://127.0.0.1:{}{}/swagger-ui.html", getPort, contextPath);
//        log.info("加载 web-swagger3地址: http://127.0.0.1:{}{}/swagger-ui/index.html", getPort, contextPath);


        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        List<String> urlList = new ArrayList<>();
        urlList.add(String.format("Local: \t\thttp://localhost:%s\n\t", port));
        urlList.add(String.format("External: \thttp://%s:%s\n\t", hostAddress, port));
        urlList.add(String.format("Doc: \t\thttp://%s:%s/doc.html\n\t", hostAddress, port));
        urlList.add(String.format("Swagger2: \thttp://%s:%s/swagger-ui.html\n\t", hostAddress, port));
        urlList.add(String.format("Swagger3: \thttp://%s:%s/swagger-ui/index.html\n\t", hostAddress, port));
        for (LazyUI lazyUI : lazyUIList) {
            if (lazyUI.support()) {
                String desc = lazyUI.desc();
                String index = lazyUI.index();
                if (index.startsWith("/")) {
                    index = index.substring(1);
                }
                urlList.add(String.format("%s: \thttp://%s:%s/%s\n\t", desc, hostAddress, port, index));
            }

        }

        Environment env = configurableApplicationContext.getEnvironment();
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        String.join("", urlList) +
                        "\n----------------------------------------------------------",
                env.getProperty("spring.application.name")
        );
    }
}
